package cola.project.hellojavaee.servlet;

import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

@WebServlet(name = "ForwardServlet" ,value = "/forward-servlet")
public class ForwardServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 将请求转发到 time-servlet
        req.getRequestDispatcher("/time-servlet").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, String[]> map = req.getParameterMap();
        System.out.println("这是在 ForwardServlet 中的数据：");
        map.forEach((k,v)->{
            System.out.println(k+":"+ Arrays.toString(v));
        });
        resp.getWriter().write("www");
        // 将请求转发到
        req.getRequestDispatcher("/time-servlet").forward(req,resp);
    }
    @Override
    public void init() throws ServletException {
        super.init();
    }
}
